package quantri.xemLog;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import utils.DateUtils;
import utils.UtilsOnScopes;

import dbutil.ConnectDB;

public class ViewLog {
	private String error;
	private java.util.Date  starDate;
	private java.util.Date endDate;
	private String userName;
	private String path ="/quantri/xemlog/emptyBieuMau.xhtml";
	private List<Table> tables;

	public ViewLog() {
	}

	public ViewLog(String error, Date starDate, Date endDate) {
		this.error = error;
		this.starDate = starDate;
		this.endDate = endDate;
	}

	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	/**************************************
	 * Method name		: view
	 * Return type		: String
	 * Decription		:Chuyen den trang xem nhat ky
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/

	public String view(){
		this.setError("");
		if(this.checkValue(this.starDate, this.endDate) == false){
			return "";
		}
		else {
		this.tables();
		System.out.println("size :"+this.tables.size());
		  if(this.tables.size() > 0){
		     this.setPath("/quantri/xemlog/bieumau.xhtml");
		  }else{
			 this.setError("Không có dữ liệu !");
			 this.setPath("/quantri/xemlog/emptyBieuMau.xhtml");
		  }
		}
		
		return "okViewLog";
	}
	/**************************************
	 * Method name		: checkValue
	 * Return type		: boolean
	 * Decription		:Kiem tra du lieu dau vao
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
	public boolean checkValue( java.util.Date star,java.util.Date  end){
		boolean result = true;
		if(star == null || star.equals("")){
			this.setError("Bạn chưa chọn ngày bắt đầu !");
			return result = false;
		}
		else if(end == null || end.equals("")){
			this.setError("Bạn chưa chọn ngày kết thúc !");
		  return result = false;
		}
		else if(star.compareTo(end) > 0){
			this.setError("Ngày kết thúc lớn hơn ngày bắt đầu !");
			return result = false;
			
		}
		return result;
	}

	/**************************************
	 * Method name		: tables
	 * Return type		: String
	 * Decription		:Hien thi thong tin o ben table
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
	public String tables() {
		List<Table> list = new ArrayList<Table>();
		Date starDateSQL = DateUtils.utilDateToSqlDate(this.starDate);
		Date endDateSQL = DateUtils.utilDateToSqlDate(this.endDate);
		String SQL = "SELECT * FROM nhatkyxoasua A inner join thanhvien B on A.thanhvien = B.id " +
				" WHERE (A.thanhvien LIKE '"
				+ userName + "' OR '"
				+ (userName.equals("-1") ? "0" : userName) + "'='0')"
				+ " AND A.thoigianxoa_sua BETWEEN ' " + starDateSQL + "' AND '"
				+ endDateSQL + "' ORDER BY  B.name,A.thoigianxoa_sua DESC";
		System.out.println(SQL);
		ArrayList<HashMap<String, Object>> array = ConnectDB
				.getDataTypeList(SQL);
		try {
			if (array.size() > 0) {
				HashMap<String, Object> hash = new HashMap<String, Object>();
				int stt = 0;
				for (int i = 0; i < array.size(); i++) {
					stt = i + 1;
					hash = (HashMap<String, Object>) array.get(i);
					Table table = new Table();
					table.setStt(String.valueOf(stt));
					
					table.setReson(hash.get("ghichu").toString());
					table.setSoHC_Madoan(hash.get("sohc_idgroup").toString());
					table.setTask(hash.get("thaotacxoa_sua").toString());
					String dateTask = DateUtils.formatDate(hash.get(
							"thoigianxoa_sua").toString());
					table.setTimeTask(dateTask);
					table.setUserName(hash.get("name").toString());
	
					list.add(table);
				}
			}
			this.setTables(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**************************************
	 * Method name		: reset
	 * Return type		: String
	 * Decription		:Rết lai du lieu
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
	public String reset() {
		UtilsOnScopes.getSession().removeAttribute("viewLog");
		return "okViewLog";
	}
	
	/**************************************
	 * Method name		: close
	 * Return type		: String
	 * Decription		:Dong poup lai
	 * Created date		: Jun 18, 2008
	 * Author			: Honglk
	 **************************************/
	public String close(){
		this.reset();
		return "cancelLog";
	}
	public List<Table> getTables() {
		this.tables();
		return tables;
	}

	public void setTables(List<Table> tables) {
		this.tables = tables;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public java.util.Date getStarDate() {
		return starDate;
	}

	public void setStarDate(java.util.Date starDate) {
		this.starDate = starDate;
	}

	public java.util.Date getEndDate() {
		return endDate;
	}

	public void setEndDate(java.util.Date endDate) {
		this.endDate = endDate;
	}

	public static void main(String[] args) {
//		ViewLog vl = new ViewLog();
//		vl.setStarDate("14/06/2008");
//		vl.setEndDate("17/06/2008");
//		vl.setUserName("-1");
//		vl.setUserName("kimhong");
//		vl.tables();
//		System.out.println(vl.getTables().size());
	}

}
